#pragma once
#include <string>


namespace U41
{
	using namespace std;

	class unordered_list
	{
	private:
		class Node
		{
		public:
			string data;
			Node *next;

			Node(string data = "", Node *next = nullptr)
			{
				this->data = data;
				this->next = next;
			}
		};

		Node *head;
		Node *freehead;
	public:

		unordered_list() : head(nullptr), freehead(nullptr)
		{
		}

		unordered_list(int size_free_list)
		{
			head = nullptr;
			for (int i = 0; i < size_free_list; i++)
			{
				freehead = new Node("", freehead);
			}
		}

		~unordered_list()
		{
			while (head != nullptr)
			{
				Node *d = head;
				head = head->next;
				delete d;
			}
			while (freehead != nullptr)
			{
				Node *d = freehead;
				freehead = freehead->next;
				delete d;
			}
		}

		void add(string str)
		{
			if (freehead != nullptr)
			{
				freehead->data = str;
				Node *n = head;
				head = freehead;
				freehead = freehead->next;
				head->next = n;
			}
			else
			{
				head = new Node(str, head);
			}
		}

		void delete_header_node()
		{
			Node *n = freehead;
			freehead = head;
			head = head->next;
			freehead->next = n;
			freehead->data = "";
		}

		void clear()
		{
			while (head != nullptr)
			{
				delete_header_node();
			}
		}

		void print()
		{
			Node *n = head;
			while (n != nullptr)
			{
				cout << n->data << ", ";
				n = n->next;
			}
		}
	};
}